package com.yyb.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.yyb.common.Result;
import com.yyb.constants.CoreConstant;
import com.yyb.enums.ResultEnums;
import com.yyb.utils.ServletUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述: [验证码的接口]
 * 
 * 
 */
@RestController
@Tag(name = "验证码相关的接口")
public class CaptchaController {
    /**
     * 得到验证码
     *   GET http://127.0.0.1:8080/captcha/getCaptcha
     *   接口ID：148742186
     *   接口地址：https://app.apifox.com/link/project/4040316/apis/api-148742186
     */
    @GetMapping(value = CoreConstant.CAPTCHA_URL)
    @Operation(summary = "获取验证码")
    public Result<String> getCaptcha(){
        //生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 1, 100);
        //得到正确的验证码
        String code = lineCaptcha.getCode();
        //把正确的验证码放到session
        ServletUtils.getSession().setAttribute(CoreConstant.CAPTCHA_KEY,code);
        System.out.println("code = " + code);
        //把验证码换成一个图片的字符串
        String base64Data = lineCaptcha.getImageBase64Data();
        return new Result<>(ResultEnums.SUCCESS,base64Data);
    }
}
